clear all

local nuts 2
use co using "$BvD_output/BvD_results_`nuts'.dta" if co!="BE" & co!="FI" & co!="IE", replace
keep co
duplicates drop co, force
drop if co=="UA"
sort co
gen Country="Austria" if co=="AT"
replace Country="Hungary" if co=="HU"
replace Country="United States" if co=="US"
replace Country="Czech Republic" if co=="CZ"
replace Country="Denmark" if co=="DK"
replace Country="Germany" if co=="DE"
replace Country="United Kingdom" if co=="GB"
replace Country="Italy" if co=="IT"
replace Country="Norway" if co=="NO"
replace Country="Poland" if co=="PL"
replace Country="Romania" if co=="RO"
replace Country="Sweden" if co=="SE"
replace Country="Portugal" if co=="PT"
replace Country="France" if co=="FR"
replace Country="Spain" if co=="ES"
replace Country="Bulgaria" if co=="BG"
sort Country

levelsof Country, local(Countrylist)
levelsof co, local(colist)

local tfpvar1 w1_lvapw
local tfpvar2 w1_log_TFPr_tfas
local tfpvar3 w1_log_TFPr_tfas

local j=1
while(`j'<=3){
*install matrixes -- add us data as default (the rest will be written over later)
matrix perm_tau_`j' = J(16, 6, .)
matrix rown perm_tau_`j' = "United States" `Countrylist'
matrix perm_xi_`j' = J(16, 6, .)
matrix rown perm_xi_`j' = "United States" `Countrylist'

matrix cov_tau_`j' = J(16, 3, .)
matrix rown cov_tau_`j' = "United States" `Countrylist'
matrix cov_xi_`j' = J(16, 3, .)
matrix rown cov_xi_`j' = "United States" `Countrylist'




*put in real values as locals
use "$BvD_output/BvD_results_`nuts'.dta" if co!="BE" & co!="FI" & co!="IE", replace
keep if tfpvar=="`tfpvar`j''"
drop if co=="UA"

sort co

expand 2 in 1
quietly replace co="US" if _n==_N


gen Country="Austria" if co=="AT"
replace Country="Hungary" if co=="HU"
replace Country="United States" if co=="US"
replace Country="Czech Republic" if co=="CZ"
replace Country="Denmark" if co=="DK"
replace Country="Germany" if co=="DE"
replace Country="United Kingdom" if co=="GB"
replace Country="Italy" if co=="IT"
replace Country="Norway" if co=="NO"
replace Country="Poland" if co=="PL"
replace Country="Romania" if co=="RO"
replace Country="Sweden" if co=="SE"
replace Country="Portugal" if co=="PT"
replace Country="France" if co=="FR"
replace Country="Spain" if co=="ES"
replace Country="Bulgaria" if co=="BG"

sort Country
gen order=_n
quietly replace order=0 if Country=="United States"
sort order

gen count_tau_prelim=count_tau
gen count_xi_prelim = count_xi
gen count_firms_prelim=count_firms


mkmat count_tau_prelim count_firms_prelim  count_tau count_firms var_tau,matrix(var_count_tau_`j') 
mkmat count_xi_prelim count_firms_prelim  count_xi count_firms var_xi,matrix(var_count_xi_`j') 


*fix Rownames for two-word names
mat rownames var_count_tau_`j' = "United States" "Austria" "Bulgaria" "Czech Republic" "Denmark" "France" "Germany" "Hungary" "Italy" "Norway" "Poland" "Portugal" "Romania" "Spain" "Sweden" "United Kingdom" 
mat rownames var_count_xi_`j' =  "United States" "Austria" "Bulgaria" "Czech Republic" "Denmark" "France" "Germany"  "Hungary" "Italy" "Norway" "Poland" "Portugal" "Romania" "Spain" "Sweden" "United Kingdom"


local j=`j'+1
}




local i=1
foreach Country in `Countrylist' {
local i=`i'+1

*covariance -- uci lci, mean
local j=1
while(`j'<=3){
	use "$BvD_output/BvD_results_loop`nuts'.dta" if co!="BE" & co!="FI" & co!="IE", replace
	gen Country="Austria" if co=="AT"
replace Country="Hungary" if co=="HU"
replace Country="United States" if co=="US"
replace Country="Czech Republic" if co=="CZ"
replace Country="Denmark" if co=="DK"
replace Country="Germany" if co=="DE"
replace Country="United Kingdom" if co=="GB"
replace Country="Italy" if co=="IT"
replace Country="Norway" if co=="NO"
replace Country="Poland" if co=="PL"
replace Country="Romania" if co=="RO"
replace Country="Sweden" if co=="SE"
replace Country="Portugal" if co=="PT"
replace Country="France" if co=="FR"
replace Country="Spain" if co=="ES"
replace Country="Bulgaria" if co=="BG"
	quietly keep if tfpvar=="`tfpvar`j''" & Country=="`Country'"
	local co=co[1]
	
	sort cov_tau
	matrix cov_tau_`j'[`i',2] = cov_taub[25]
	matrix cov_tau_`j'[`i',3] = cov_taub[975]
	sort cov_xi
	matrix cov_xi_`j'[`i',2] = cov_xib[25]
	matrix cov_xi_`j'[`i',3] = cov_xib[975]
	
	collapse (mean) cov*
	matrix cov_tau_`j'[`i',1]=cov_taub[1]
	matrix cov_xi_`j'[`i',1]=cov_xib[1]
	local j=`j'+1
	}
*permutations -- uci, lci, mean.
use "$BvD_output/permute_out_stats_`co'`nuts'.dta" if co!="BE" & co!="FI" & co!="IE", clear
gen Country="Austria" if co=="AT"
replace Country="Hungary" if co=="HU"
replace Country="United States" if co=="US"
replace Country="Czech Republic" if co=="CZ"
replace Country="Denmark" if co=="DK"
replace Country="Germany" if co=="DE"
replace Country="United Kingdom" if co=="GB"
replace Country="Italy" if co=="IT"
replace Country="Norway" if co=="NO"
replace Country="Poland" if co=="PL"
replace Country="Romania" if co=="RO"
replace Country="Sweden" if co=="SE"
replace Country="Portugal" if co=="PT"
replace Country="France" if co=="FR"
replace Country="Spain" if co=="ES"
replace Country="Bulgaria" if co=="BG"
drop if iter==0
local j=1
while(`j'<=3){
sort var_tau_`j'
matrix perm_tau_`j'[`i',2] = var_tau_`j'[25]
matrix perm_tau_`j'[`i',3] = var_tau_`j'[975]

sort var_xi_`j'
matrix perm_xi_`j'[`i',2] = var_xi_`j'[25]
matrix perm_xi_`j'[`i',3] = var_xi_`j'[975]
local j=`j'+1
}
collapse (mean) var*
local j=1
while(`j'<=3){
matrix perm_tau_`j'[`i',1] = var_tau_`j'[1]
matrix perm_xi_`j'[`i',1] =var_xi_`j'[1]
local j=`j'+1
}

*i is now number of countries in list, inclusive of US.
*iminus1 is now alphabetical ordering of countries
local iminus1 = `i'-1

}
use "$BvD_output/BvD_results_`nuts'.dta" if co!="BE" & co!="FI" & co!="IE", replace
gen Country="Austria" if co=="AT"
replace Country="Hungary" if co=="HU"
replace Country="United States" if co=="US"
replace Country="Czech Republic" if co=="CZ"
replace Country="Denmark" if co=="DK"
replace Country="Germany" if co=="DE"
replace Country="United Kingdom" if co=="GB"
replace Country="Italy" if co=="IT"
replace Country="Norway" if co=="NO"
replace Country="Poland" if co=="PL"
replace Country="Romania" if co=="RO"
replace Country="Sweden" if co=="SE"
replace Country="Portugal" if co=="PT"
replace Country="France" if co=="FR"
replace Country="Spain" if co=="ES"
replace Country="Bulgaria" if co=="BG"
sort Country

**Here we manually input numbers for the US from the RDC. 
**These are most easily available from the output for Figures 4 and A.3



matrix var_count_xi_1[1,5]=.0632
matrix var_count_xi_2[1,5]=.0255
matrix var_count_xi_3[1,5]=.0255
matrix var_count_tau_1[1,5]=.290
matrix var_count_tau_2[1,5]=.1096 
matrix var_count_tau_3[1,5]=.1096 

matrix cov_tau_1[1,1]=.0405
matrix cov_tau_1[1,2]=.0324
matrix cov_tau_1[1,3]=.0484

matrix cov_tau_2[1,1]=.0232
matrix cov_tau_2[1,2]=.0186
matrix cov_tau_2[1,3]=.0270

matrix cov_tau_3[1,1]=.0232
matrix cov_tau_3[1,2]=.0186
matrix cov_tau_3[1,3]=.0270

matrix cov_xi_1[1,1]=.0158
matrix cov_xi_1[1,2]=.0117
matrix cov_xi_1[1,3]=.0197

matrix cov_xi_2[1,1]=.0081
matrix cov_xi_2[1,2]=.0061
matrix cov_xi_2[1,3]=.0098

matrix cov_xi_3[1,1]=.0081
matrix cov_xi_3[1,2]=.0061
matrix cov_xi_3[1,3]=.0098

matrix perm_xi_1[1,1]=0.0465
matrix perm_xi_1[1,2]=0.0366
matrix perm_xi_1[1,3]=0.0597

matrix perm_xi_2[1,1]=0.0170
matrix perm_xi_2[1,2]=0.0127
matrix perm_xi_2[1,3]=0.0233

matrix perm_xi_3[1,1]=0.0170
matrix perm_xi_3[1,2]=0.0127
matrix perm_xi_3[1,3]=0.0233

matrix perm_tau_1[1,1]=.2672
matrix perm_tau_1[1,2]=0.2456
matrix perm_tau_1[1,3]=0.2905

matrix perm_tau_2[1,1]=.0960
matrix perm_tau_2[1,2]=0.0869
matrix perm_tau_2[1,3]=0.108

matrix perm_tau_3[1,1]=.0960
matrix perm_tau_3[1,2]=0.0869
matrix perm_tau_3[1,3]=0.108


local j=1
while(`j'<=3){
 if(`j'==2){
 local j=2
 coefplot  (matrix(var_count_tau_`j'[,5]), label("Empirical: Raw Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i})") offset(-.15) mcolor(black) msize(small) recast(connected) lwidth(.075)  ) ///
 (matrix(perm_tau_`j'[,1]), msymbol(D) label("Permutations: Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i})") mfcolor(none) mcolor(red) msize(small) offset(0) ci((perm_tau_`j'[,2] perm_tau_`j'[,3])) ciopts(lpattern(dash) lcolor(red) ) recast(connected) lcolor(red) lstyle(solid) lwidth(.075) ) ///
 (matrix(cov_tau_`j'[,1]), label("Bias-Corrected: Cov(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:A},`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:B}) = Var(`=ustrunescape("\u03C4")'{sub:l,i})"  ) msymbol(T) msize(small) mcolor(blue) offset(.15) ci((cov_tau_`j'[,2] cov_tau_`j'[,3])) ciopts(lpattern(solid) lcolor(blue)) recast(connected) lcolor(blue) lstyle(solid) lwidth(.075) )  ///
 ,ytitle("Variance: Industry-Specific Location Effects") ylabel(,angle(horizontal)  nogrid) ysize(4) vertical xsize(10)  yline(0) graphregion(color(white)) bgcolor(white)  xlabel(,angle(45) nogrid notick) xtick(1.5(1)15.5) ///
grid(between) ///
 legend( c(1) position(10) ring(0)  region(lcolor(none) color(none))   ///
			 keygap(*.75)  )  
	graph export "$BvD_exhibits/Fig_6_tau.pdf", replace
 coefplot  (matrix(var_count_xi_`j'[,5]),label("Empirical: Raw Var(`=ustrunescape("\u03BE\u0302")'{sub:l})") offset(-.15) mcolor(black) msize(small)  recast(connected) lwidth(.075)  ) ///
 (matrix(perm_xi_`j'[,1]),msymbol(D)  label("Permutations: Var(`=ustrunescape("\u03BE\u0302")'{sub:l})") mfcolor(none) mcolor(red) msize(small) offset(0) ci((perm_xi_`j'[,2] perm_xi_`j'[,3])) ciopts(lpattern(dash) lcolor(red))  recast(connected) lcolor(red) lstyle(solid) lwidth(.075) )  ///
 (matrix(cov_xi_`j'[,1]), label("Bias-Corrected: Cov(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:A},`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:B}) = Var(`=ustrunescape("\u03BE")'{sub:l})"   ) msymbol(T) msize(small) mcolor(blue) offset(.15) ci((cov_xi_`j'[,2] cov_xi_`j'[,3])) ciopts(lpattern(solid) lcolor(blue)) recast(connected) lcolor(blue) lstyle(solid) lwidth(.075) ) ///
 ,ytitle("Variance: Location Effects")  ylabel(,angle(horizontal)  nogrid) vertical xsize(10)  yline(0) graphregion(color(white)) bgcolor(white)  xlabel(,angle(45) nogrid notick ) xtick(1.5(1)15.5) ///
 grid(between) ///
 legend( c(1)  position(10) ring(0) region(lcolor(none) color(none))   ///
		 keygap(*.75)   )  recast(connected)
 graph export "$BvD_exhibits/Fig_6_xi.pdf", replace
 }
 
 if(`j'==1){
 local j=1
 local outdate=12222020
 coefplot  (matrix(var_count_tau_`j'[,5]), label("Empirical: Raw Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i})") offset(-.15) mcolor(black) recast(connected) lwidth(.075) )  ///
 (matrix(perm_tau_`j'[,1]), msymbol(D) label("Permutations: Var(`=ustrunescape("\u03C4\u0302")'{sub:l,i})") mfcolor(none) mcolor(red) offset(0) ci((perm_tau_`j'[,2] perm_tau_`j'[,3])) ciopts(lpattern(dash) lcolor(red)) recast(connected) lcolor(red) lstyle(solid) lwidth(.075) ) ///
 (matrix(cov_tau_`j'[,1]), label("Bias-Corrected: Cov(`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:A},`=ustrunescape("\u03C4\u0302")'{sub:l,i}{sup:B}) = Var(`=ustrunescape("\u03C4")'{sub:l,i})"  ) msymbol(T) mcolor(blue) offset(.15) ci((cov_tau_`j'[,2] cov_tau_`j'[,3])) ciopts(lpattern(solid) lcolor(blue)) recast(connected) lcolor(blue) lstyle(solid) lwidth(.075) )  ///
 ,ytitle("Variance: Industry-Specific Location Effects") ytick(0(.1).3)  ylabel(-.05(.05).3,angle(horizontal) nogrid )  vertical xsize(10)  yline(0) graphregion(color(white)) bgcolor(white)  xlabel(,angle(45) nogrid notick) xtick(1.5(1)15.5) ///
grid(between) ///
 legend( c(1) position(2) ring(0)  region(lcolor(none) color(none))   ///
			 keygap(*.75)   )
  graph export "$BvD_exhibits/Fig_A4_tau.pdf", replace
  
 coefplot  (matrix(var_count_xi_`j'[,5]),label("Empirical: Raw Var(`=ustrunescape("\u03BE\u0302")'{sub:l})") offset(-.15) mcolor(black) recast(connected) lwidth(.075) ) ///
 (matrix(perm_xi_`j'[,1]),msymbol(D)  label("Permutations: Var(`=ustrunescape("\u03BE\u0302")'{sub:l})") mfcolor(none) mcolor(red) offset(0) ci((perm_xi_`j'[,2] perm_xi_`j'[,3])) ciopts(lpattern(dash) lcolor(red))  recast(connected) lcolor(red) lstyle(solid) lwidth(.075)  )  ///
 (matrix(cov_xi_`j'[,1]), label("Bias-Corrected: Cov(`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:A},`=ustrunescape("\u03BE\u0302")'{sub:l}{sup:B}) = Var(`=ustrunescape("\u03BE")'{sub:l})"   ) msymbol(T) mcolor(blue) offset(.15) ci((cov_xi_`j'[,2] cov_xi_`j'[,3])) ciopts(lpattern(solid) lcolor(blue)) recast(connected) lcolor(blue) lstyle(solid) lwidth(.075) ) ///
 ,ytitle("Variance: Location Effects") ytick(-.04(.02).1) ylabel(-.04(.02).1,angle(horizontal) nogrid) vertical xsize(10)  yline(0) graphregion(color(white)) bgcolor(white)  xlabel(,angle(45) nogrid notick ) xtick(1.5(1)15.5) ///
 grid(between) ///
 legend( c(1)  position(10) ring(0) region(lcolor(none) color(none))   ///
		 keygap(*.75)  )  
 graph export "$BvD_exhibits/Fig_A4_xi.pdf", replace
 }
 
 local j=`j'+1
 }
 
